/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/page/page",
	"scprit/followup/app/workBench/workBenchDiabetes.servers.js",
	"scprit/followup/app/workBench/workBenchDiabetes.ui.js"
	],
	function($,$bb,$page,$workBenchServers,$workBenchUi){
	var event = {};
	/**
	 * 点击"添加患者"按钮跳转到"添加患者"页面
	 */
	var onAddPatientClick = function(){
		$bb.openPage("addPatient");
	};

	/**
	 * 点击今日门诊患者跳转
	 */
	event.onBabyClick = function(){
		var rows = $(this).data("value");
		var index = $(this).index();
		var params = {
			patientId: rows[index].patientId,
			followQueueId:rows[index].followQueueId,
			patientSchemeId:rows[index].ptSchemeid,
			queuename:rows[index].followQueueName
		};
		$bb.openPage("motionAndBG",params);
	}

	/**
	 * 点击全部患者卡片进入详情页
	 */
	var onBabyCardClick = function(){
		var patientId = $(this).attr("patientId");
		var followQueueId = $(this).attr('followQueueId');
		var patientSchemeId = $(this).attr('patientSchemeId');
		var queuename = $(this).find('.babycard-queuename').html();
		var params = {
			patientId: patientId,
			followQueueId:followQueueId,
			patientSchemeId:patientSchemeId,
			queuename:queuename
		};
		$bb.openPage("motionAndBG",params);
	};

	/**
	 * 按科研编号、手机号、姓名搜索患者时，传参有所不同
	 */
	var searchKeywordAjax = function(params){
		var reg = /^1\d{10}$/;
		if(reg.test(params.fuzzyVar)){
			params = $.extend(params,{phoneNum:params.fuzzyVar});
		}
		$workBenchServers.ajaxPatientsQueue(params,ajaxPatientsQueueCallBack);
	};
	/**
	 * 输入姓名、手机号、科研编号进行搜索
	 */
	onSearchItem = function(){
		var data = $(this).data();
		var keyword = $("#searchCon input").val();
		var queueId = $("#selectQueue select").val();
		var params = {
			"flag":data.flag,
			"rows":data.rows,
			"page":1,
			"fuzzyVar": $.trim(keyword),
			"queueId":queueId
		};
		searchKeywordAjax(params);
	}
	/**
	 * 请求全部患者成功后的回调函数
	 */
	var ajaxPatientsQueueCallBack = function(data,params){
		if(data.rows && data.rows.length >= 0){
			var pagesAll = Math.ceil(data.total / params.rows);
			if(data.rows.length < params.rows){
				var empty = [];
				for(var i = 0;i< (params.rows -data.rows.length) ;i++){
					empty.push({});
				}
			}
			$workBenchUi.babyListInit({
				id:"windowBabyList",
				list:data.rows,
				empty:empty
			});
			/* 翻页 */
			$page.turnInit({
				id:"windowBottomPage",
				pagesAll:pagesAll,
				page:params.page
			},$workBenchServers.ajaxPatientsQueue,params,ajaxPatientsQueueCallBack);

			/* 点击随访患者卡片进入详情页 */
			$("#windowBabyList .clickbabycard").unbind("click");
			$("#windowBabyList .clickbabycard").click(onBabyCardClick);
			/* 点击添加 */
			$(".dashboard-add #addPatient").click(onAddPatientClick);
		}
		/* 姓名、手机号码查询 */
		$("#searchCon input").unbind("input propertychange");
		$("#searchCon input").data(params);
		$("#searchCon input").bind("input propertychange",onSearchItem);
		/* 根据队列名称查询 */
		$("#selectQueue select").unbind("change");
		$("#selectQueue select").data(params);
		$("#selectQueue select").change(onSearchItem);
		/* 查询按钮 */
		$("#toSearch").off('click');
		$("#toSearch").data(params);
		$("#toSearch").click(onSearchItem);
	};
	event.ajaxPatientsQueueCallBack = function(data,params){
		ajaxPatientsQueueCallBack(data,params);
	}
	
	return event;
});
